package org.dromara.work.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysXzqhBo;
import org.dromara.system.domain.vo.SysXzqhVo;
import org.dromara.system.service.ISysXzqhService;
import org.dromara.work.domain.bo.DmBaseComplexSaveBo;
import org.dromara.work.domain.bo.DmBaseQueryBo;
import org.dromara.work.domain.vo.*;
import org.dromara.work.service.IDmBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 标准地名基础库入库变更
 *
 * @author Chen
 *  2024-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/library/dm")
public class DmRkController extends BaseController {

    @Resource
    private final IDmBaseService iDmBaseService;

    @Resource
    private final ISysXzqhService xzqhService;
    /**
     * 地名基础信息查询
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/pageQuery")
    public TableDataInfo<DmBaseBriefVo> page(@Validated(QueryGroup.class) DmBaseQueryBo bo, PageQuery pageQuery) {
        return iDmBaseService.customPageList(bo, pageQuery);
    }

    /**
     * 导出地名属性信息列表
     */
    @SaCheckPermission("library:dm:export")
    @PostMapping("/export")
    public void export(DmBaseQueryBo bo, HttpServletResponse response) {
        List<DmBaseVo> list = iDmBaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "地名属性信息", DmBaseVo.class, response);
    }


    /**
     * 获取地名属性信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/{id}")
    @SaIgnore
    public R<DmBaseComplexVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable String id) {
        return R.ok(iDmBaseService.queryById(id));
    }
    /**
     * 获取地名属性信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/text/{id}")
    public R<DmBaseFlowComplexVo> getTextInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable String id) {
        return R.ok(iDmBaseService.queryFlowById(id));
    }

    /**
     * 新增地名属性信息
     */
    @SaCheckPermission("library:dm:add")
    @PostMapping()
    @RepeatSubmit()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DmBaseComplexSaveBo bo) {
        String code=bo.getBase().getCode();
        String str=bo.getBase().getXzqh()+""+bo.getBase().getDmlb();
        if(!code.startsWith(str)){
            return R.fail("地名编码错误，须以【"+str+"】开头。");
        }
        return toAjax(iDmBaseService.insertByBo(bo));
    }

    /**
     * 修改地名属性信息
     */
    @SaCheckPermission("library:dm:edit")
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DmBaseComplexSaveBo bo) {
        return toAjax(iDmBaseService.updateByBo(bo));
    }

    /**
     * 删除地名属性信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("library:dm:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iDmBaseService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 生成一个地名编码
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/nextCode")
    public R<DmBaseBriefVo> getNextCode(DmBaseQueryBo bo) {
        if(StringUtils.isBlank(bo.getXzqh())){
            return R.fail("请输入行政区划代码。");
        }
        if(StringUtils.isBlank(bo.getDmlb())){
            return R.fail("请输入地名类别代码。");
        }
        return R.ok(iDmBaseService.generateCode(bo));
    }

    /**
     * 统计地名类别
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/stat")
    public TableDataInfo<DmStatVo> stat(DmBaseQueryBo bo) {
        return iDmBaseService.queryDmlbByXzq(bo.getXzqh());
    }

    /**
     * 统计地名类型
     *
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/stat/dmlb")
    public TableDataInfo<DmStatVo> statLb(@Validated(QueryGroup.class) DmBaseQueryBo bo) {
        String name= LoginHelper.getDeptName();
        SysXzqhBo sysXzqhBo =new SysXzqhBo();
        List<SysXzqhVo> list= new ArrayList<>();
        String xzq="";
        if(StringUtils.isNotBlank(name)){
            sysXzqhBo.setName(name);
            list=xzqhService.queryList(sysXzqhBo);
        }
        if(!list.isEmpty()){
            xzq=list.get(0).getId()+"";
        }
        if(StringUtils.isNotBlank(bo.getXzqh())){
            xzq=bo.getXzqh();
        }
        return iDmBaseService.queryDmlbByXzq(xzq);
    }

    /**
     * 统计地名类型
     *
     */
    @SaCheckPermission("library:dm:list")
    @GetMapping("/stat/qhdm")
    public TableDataInfo<DmStatVo> statQhdm(@Validated(QueryGroup.class) DmBaseQueryBo bo) {
        String name=LoginHelper.getDeptName();
        SysXzqhBo sysXzqhBo=new SysXzqhBo();
        List<SysXzqhVo> list= new ArrayList<>();
        String xzq="";
        if(StringUtils.isNotBlank(name)){
            sysXzqhBo.setName(name);
            list=xzqhService.queryList(sysXzqhBo);
        }
        if(!list.isEmpty()){
            xzq=list.get(0).getId()+"";
        }
        if(StringUtils.isNotBlank(bo.getXzqh())){
            xzq=bo.getXzqh();
        }
        return iDmBaseService.queryQhdmByXzq(xzq);
    }





}
